package com.ec.mn.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ec.common.domain.Result;
import com.ec.mn.entity.HealthRecord;
import com.ec.mn.entity.Notification;
import com.ec.mn.service.NotificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "通知管理")
@RestController
@RequestMapping("/notifications")
public class NotificationController {
    @Autowired
    private NotificationService notificationService;

    @ApiOperation("新增通知")
    @PostMapping("/add")
    public Result<Notification> addNotification(@RequestBody Notification notification) {
        if (!notificationService.validateNotification(notification)) {
            return Result.error("通知数据验证失败");
        }
        boolean success = notificationService.save(notification);
        return success ? Result.ok(notification) : Result.error("新增通知失败");
    }

//    @ApiOperation("健康异常数据触发通知")
//    @PostMapping("/addByHealthRecord")
//    public Result<Void> addByHealthRecord(@RequestBody HealthRecord healthRecord) {
//        notificationService.add(healthRecord, warningRulesService.checkAndWarn(healthRecord));
//        return Result.ok();
//    }

    @ApiOperation("根据ID删除通知")
    @DeleteMapping("/{id}")
    public Result<Void> deleteNotification(@ApiParam("通知ID") @PathVariable Long id) {
        boolean success = notificationService.removeById(id);
        return success ? Result.ok() : Result.error("删除失败");
    }

    @ApiOperation("批量删除通知")
    @DeleteMapping("/batch")
    public Result<Void> batchDeleteNotification(@RequestBody List<Long> ids) {
        boolean success = notificationService.removeByIds(ids);
        return success ? Result.ok() : Result.error("批量删除失败");
    }

    @ApiOperation("更新通知")
    @PostMapping("/update")
    public Result<Notification> updateNotification( @RequestBody Notification notification) {

        boolean success = notificationService.updateById(notification);
        return success ? Result.ok(notification) : Result.error("更新失败");
    }

    @ApiOperation("根据ID查询通知")
    @GetMapping("/search/{id}")
    public Result<Notification> getNotificationById(@ApiParam("通知ID") @PathVariable Long id) {
        Notification notification = notificationService.getById(id);
        return notification != null ? Result.ok(notification) : Result.error("通知不存在");
    }
    //以下没有验证
//
//    @ApiOperation("根据接收者ID查询通知")
//    @GetMapping("/receiver/{receiverId}")
//    public Result<List<Notification>> getNotificationsByReceiverId(@ApiParam("接收者ID") @PathVariable Long receiverId) {
//        return Result.ok(notificationService.getByReceiverId(receiverId));
//    }
//
//    @ApiOperation("根据发送者ID查询通知")
//    @GetMapping("/sender/{senderId}")
//    public Result<List<Notification>> getNotificationsBySenderId(@ApiParam("发送者ID") @PathVariable Long senderId) {
//        return Result.ok(notificationService.getBySenderId(senderId));
//    }
//
//    @ApiOperation("查询未读通知")
//    @GetMapping("/unread/{receiverId}")
//    public Result<List<Notification>> getUnreadNotifications(@ApiParam("接收者ID") @PathVariable Long receiverId) {
//        return Result.ok(notificationService.getUnreadByReceiverId(receiverId));
//    }
//
//    @ApiOperation("查询已读通知")
//    @GetMapping("/read/{receiverId}")
//    public Result<List<Notification>> getReadNotifications(@ApiParam("接收者ID") @PathVariable Long receiverId) {
//        return Result.ok(notificationService.getReadByReceiverId(receiverId));
//    }
//
//    @ApiOperation("分页查询通知")
//    @GetMapping("/page/{receiverId}")
//    public Result<IPage<Notification>> getNotificationPage(
//            @ApiParam("接收者ID") @PathVariable Long receiverId,
//            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
//            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size) {
//        Page<Notification> page = new Page<>(current, size);
//        return Result.ok(notificationService.getPageByReceiverId(page, receiverId));
//    }
//
//    @ApiOperation("标记通知为已读")
//    @PutMapping("/{id}/read")
//    public Result<Void> markAsRead(@ApiParam("通知ID") @PathVariable Long id) {
//        boolean success = notificationService.markAsRead(id);
//        return success ? Result.ok() : Result.error("标记已读失败");
//    }
//
//    @ApiOperation("标记通知为已删除")
//    @PutMapping("/{id}/delete")
//    public Result<Void> markAsDeleted(@ApiParam("通知ID") @PathVariable Long id) {
//        boolean success = notificationService.markAsDeleted(id);
//        return success ? Result.ok() : Result.error("标记删除失败");
//    }
//
//    @ApiOperation("统计未读通知数量")
//    @GetMapping("/count/unread/{receiverId}")
//    public Result<Long> countUnreadNotifications(@ApiParam("接收者ID") @PathVariable Long receiverId) {
//        return Result.ok(notificationService.countUnreadByReceiverId(receiverId));
//    }
//
//    @ApiOperation("统计已读通知数量")
//    @GetMapping("/count/read/{receiverId}")
//    public Result<Long> countReadNotifications(@ApiParam("接收者ID") @PathVariable Long receiverId) {
//        return Result.ok(notificationService.countReadByReceiverId(receiverId));
//    }
} 